/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[SerializeObjectToTextFile.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package io.serialization;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

/**
 * 以文本的方式存储对象
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class SerializeObjectToTextFile {

	/**
	 * 写出数据
	 * 
	 * @param out
	 * @param employee
	 */
	private void writeData(PrintWriter out, Employee employee) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(employee.getHireDate());
		out.println(employee.getName() + "|" + employee.getSalary() + "|" + calendar.get(Calendar.YEAR) + "|"
				+ (calendar.get(Calendar.MONTH) + 1) + "|" + calendar.get(Calendar.DAY_OF_MONTH));
	}

	/**
	 * 读入数据
	 * 
	 * @param scanner
	 */
	private void readData(Scanner scanner) {
		String line = scanner.nextLine();
		String[] tokens = line.split("\\|");
		Employee employee = new Employee(tokens[0], Double.parseDouble(tokens[1]), Integer.parseInt(tokens[2]),
				Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4]));
		System.out.println(employee);
	}

	/**
	 * 将每一个员工信息输出到文件
	 * 
	 * @param employees
	 * @param out
	 * @throws IOException
	 */
	public void write(PrintWriter out, Employee[] employees) throws IOException {
		out.println("Employee numbers : " + employees.length);
		for (Employee employee : employees) {
			writeData(out, employee);
		}
	}

	/**
	 * 从文件读入每一个员工信息
	 * 
	 * @param scanner
	 * @throws IOException
	 */
	public void read(Scanner scanner) throws IOException {
		String info = scanner.nextLine();
		System.out.println(info);

		while (scanner.hasNext()) {
			readData(scanner);
		}
	}

	public static void main(String[] args) throws FileNotFoundException, IOException {
		Employee[] employees = new Employee[3];
		employees[0] = new Employee("Sean", 100000, 1984, 3, 24);
		employees[1] = new Employee("CC", 100000, 1985, 4, 24);
		employees[2] = new Employee("Yang", 100000, 1986, 5, 24);

		SerializeObjectToTextFile d = new SerializeObjectToTextFile();

		PrintWriter out = new PrintWriter("employee.txt");
		d.write(out, employees);
		out.close();

		Scanner scanner = new Scanner(new FileReader("employee.txt"));
		d.read(scanner);
		scanner.close();

	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class Employee {

	private String name;
	private double salary;
	private Date hireDate;

	public Employee(String name, double salary, int year, int month, int day) {
		this.name = name;
		this.salary = salary;
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		hireDate = calendar.getTime();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public Date getHireDate() {
		return hireDate;
	}

	public void setHireDate(Date hireDate) {
		this.hireDate = hireDate;
	}

	@Override
	public String toString() {
		return getClass().getName() + " [name=" + name + ", salary=" + salary + ", hireDate=" + hireDate + "]";
	}

	public void raiseSalary(double byPercent) {
		double raise = salary * byPercent / 100;
		salary += raise;
	}
}